package com.talent.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;

/**
 * Created by zhumenglong on 2016/9/22.
 */
@Controller
@RequestMapping("/jedis")
public class JedisController {

    // inject the actual template
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    // inject the template as ListOperations
    // can also inject as Value, Set, ZSet, and HashOperations
    // http://www.cnblogs.com/luochengqiuse/p/4638988.html 解释了为什么可以注入，毕竟两者是毫无父子兄弟关系的两个类。
    // http://www.bubuko.com/infodetail-966826.html
    @Resource(name="redisTemplate")
    private ListOperations<String, String> listOps;

    @RequestMapping("addLink/{userId}/{url}")
    public String addLink(@PathVariable("userId") String userId, @PathVariable("url")String url) {
        listOps.leftPush(userId, url);
        // or use template directly
        redisTemplate.boundListOps(userId).leftPush(url);
        return "ok";
    }
}
